package com.example.newfragment;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.adapter.CTHAdapter;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.weitown.fragment.HomeList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class HomeNewMenulistAdapter extends CTHAdapter<HomeList> {

	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	public HomeNewMenulistAdapter(Context context, List<HomeList> data,
			DisplayImageOptions options, ImageLoader imageLoader) {
		super(context, data);
		this.options = options;
		this.imageLoader = imageLoader;

	}

	@Override
	public int getCount() {

		return super.getCount();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HolderViewStatic viewContent = null;
		HomeList homeList = data.get(position);

		if (convertView == null) {

			viewContent = new HolderViewStatic();

			convertView = mInflater.inflate(R.layout.homemenulist_litem, null);

			viewContent.iv_image = (ImageView) convertView.findViewById(R.id.iv_homeltem_image);
			viewContent.tv_title = (TextView) convertView.findViewById(R.id.tv_homeltem_title);
			viewContent.tv_content = (TextView) convertView.findViewById(R.id.tv_homeltem_content);
			viewContent.tv_time = (TextView) convertView.findViewById(R.id.tv_homeltem_time);
			viewContent.tv_homeltem_comefrom = (TextView) convertView.findViewById(R.id.tv_homeltem_comefrom);
			viewContent.tv_unreadnum = (TextView) convertView.findViewById(R.id.tv_unreadnum);
//			viewContent.image_priorty = (ImageView) convertView.findViewById(R.id.image_priority);

			convertView.setTag(viewContent);
		} else {
			viewContent = (HolderViewStatic) convertView.getTag();
		}

		if (homeList != null) {
			String ownerName = homeList.getOwner_name();
			String title = homeList.getTitle();
			String updateTime = homeList.getUpdate_time();
			String comefrom = homeList.getComefrom();
			String caseid = homeList.getSource_id();
//			String priority = homeList.getPriority();
//			if("1".equals(priority)){
//				Log.e("priority", priority);
//				viewContent.image_priorty.setImageResource(R.drawable.priorty01);
//			}else if("2".equals(priority)){
//				Log.e("priority", priority);
//				viewContent.image_priorty.setImageResource(R.drawable.priorty02);
//			}else if("3".equals(priority)){
//				Log.e("priority", priority);
//				viewContent.image_priorty.setImageResource(R.drawable.priorty03);
//			}else if("4".equals(priority)){
//				Log.e("priority", priority);
//				viewContent.image_priorty.setImageResource(R.drawable.priorty04);
//			}else if("5".equals(priority)){
//				Log.e("priority", priority);
//				viewContent.image_priorty.setImageResource(R.drawable.priorty05);
//			}else if("6".equals(priority)){
//				Log.e("priority", priority);
//				viewContent.image_priorty.setImageResource(R.drawable.priorty06);
//			}else{
//				viewContent.image_priorty.setImageResource(R.drawable.priorty03);
//			}
			viewContent.tv_title.setText(ownerName);
			viewContent.tv_content.setText(title);
			if(updateTime != null && (!"".equals(updateTime))){
				String time = updateTime.substring(5, 16);
				viewContent.tv_time.setText(time);
			}else{
				viewContent.tv_time.setText("");
			}
			viewContent.tv_homeltem_comefrom.setText("来自："+comefrom);
			
			int unreadNum = SharePreferenceHelper.getUnreadNum(context, caseid);
			if(unreadNum > 0){
				viewContent.tv_unreadnum.setVisibility(View.VISIBLE);
				viewContent.tv_unreadnum.setText("" + unreadNum);
			}else{
				viewContent.tv_unreadnum.setVisibility(View.GONE);
			}
			
			
			options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.default_header)
			.showImageForEmptyUri(R.drawable.default_header)
			.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
			.cacheOnDisc(true).considerExifParams(true).cacheOnDisk(true)
			.build();
			String owner_avatar = homeList.getOwner_avatar();
			imageLoader.displayImage(owner_avatar,viewContent.iv_image);
		}
		
		
		return convertView;
	}

	static class HolderViewStatic {
		ImageView iv_image;
		TextView tv_title;// 标题
		TextView tv_content;// 内容
		TextView tv_time;// 时间
		TextView tv_homeltem_comefrom;// 来源
		TextView tv_unreadnum;//未读消息数
		ImageView image_priorty; //
	}

}
